class Solution {
    public int minOperations(int[] nums, int x) {
        int n = nums.length;
        int sum = 0;
        for (int a : nums) {
            sum += a;
        }
        int target = sum - x;
        // 只有数组中的数全都大于0这里才能使用滑动窗口
        if (target < 0) {
            return -1;
        }
        int len = -1;
        sum = 0;
        for (int left = 0, right = 0; right < n; right ++) {
            // 进窗口
            sum += nums[right];
            while (sum > target) {
                // 判断
                sum -= nums[left++];// 出窗口

            }
            if (sum == target) {
                len = Math.max(len, right - left + 1);// 更新结果
            }
        }
        if (len == -1) {
            return len;
        } else {
            return n - len;
        }
    }
}